$(function(){
		//查询相关人员的项目信息
	loadFileList(parentId);
	$("ul li a[href$='projectFileList.jsp']").addClass("active");
	//点击文件名
	$("#projectFieList").on("click",".folderCls",function(e){
		e.preventDefault();
		e.stopPropagation();			//阻止冒泡
		
		var fileId = $(this).attr("key");
		var folderType = $(this).attr("folderType");
		
		clickFolderFn(fileId,isFolderFn(folderType));
	});
	//下载 
	$("#projectFieList").on("click",".downupLoad",function(e){
		e.preventDefault();
		e.stopPropagation();			//阻止冒泡
		clickFolderFn($(this).attr("key"),false);
	});
	
	
	
	
	$('#projectFieList').on('click','.moveFrimLibrary',function(e){
		e.stopPropagation();
		e.preventDefault();
		moveFrimLibrary($(this).attr('uuid'));
	})
	
	
	//给目录导航条添加事件
	
	$("#navigationProject").on("click","a",function(e){
		e.preventDefault();
		var key = $(this).attr("key");
		clickFolderFn(key,true);
	});
	//点击全部文件的事件导航条
	$("#skipProject").on("click","a",function(e){
		e.preventDefault();
		clickFolderFn("",true);
	});
	
	
	
	$('#projectFieList').on('click','.selectCheck',function(e){
		e.stopPropagation();			//阻止冒泡
		showCheckedCount();
	});
	
	//选中所有的checkBox 事件代表选中所有
	$('table th input:checkbox').on('click', function(){
		var p = this;
		$(p).closest('table').find('tr > td:first-child input:checkbox').each(function(){
			this.checked = p.checked;
		});
		
		showCheckedCount();		//修改选中数
	});

	
	
	//删除 事件
	$("#fileName").on("click",".buttonDelete",remove);

	
	$.extend(Otod.file.defaultOptions,{
		url: Global.ctx + "/servlet/upload?type=interfaceManage&beanName=cdDocFileService",
		type:"commonManage",
		isBusid:false,
		sending: function(file, xhr, formData){
			formData.append("parentId", parentId);
			formData.append("fileType",40040002);
		},
		success: function(file, data, e) {
			loadFileList(parentId);
		},
		error: function(file, message){
			Otod.Msg.error(message.error);
			$("#fileActive").hide();
			document.getElementById("progress").style.width = 0 + "%";
		},
	});
//	Otod.file.init();
	
	//可拖拽到当前页面上传
	Otod.file.init(document.body,{
		clickable:false,
		success: function(file, data, e) {
			location.reload();
		},
		//进度条
		uploadprogress:function(file,progress, bytesSent) {			
			$("#fileActive").show();
			document.getElementById("progress").style.width = progress + "%";
		}
	});	
	
	//点击按钮上传
	Otod.file.init($(".btn-upload")[0],{
		clickable:true,
		success: function(file, data, e) {			
			location.reload();
		},
		//进度条
		uploadprogress:function(file,progress, bytesSent) {
			$("#fileActive").show();
			document.getElementById("progress").style.width = progress + "%";
			/*if (progress == 100) {
				$("#fileActive").hide();
				document.getElementById("progress").style.width = 0 + "%";
			}*/
		}
	});	

	
	
});



//运行页面查询的方法
//parentId 上级目录的id
function loadFileList(parentId){
	//用于存储导航条的名字和id
	Otod.ajax({
		url: ctx + '/cd/doc/queryForList.do',
		data:{parentId:parentId,type:"40040002"},
		type:"GET",
		successFn : function(data){	
			var vo = data.cdDocFolderVo;
			buildCrumbs(vo);
			readerProjectFile(data.childrenList);
		}
	});
}

/**
 * 读取数据 生成html
 * @param data
 */
function readerProjectFile(data){
	
	var showProjectFile = $('#showProjectFileListMtp').html();
	Mustache.parse(showProjectFile);
	
	var itemMtp = $('#itemMtp').html();

	var rendered = Mustache.render(showProjectFile,{
		data: data,
		isfolder: function(){					//是否为文件夹
			return isFolderFn(this.folderType);
		},
		formatDt: function(){
			return function(text,render){
				return DateUtil.dateFormat(+render(text));		//+render(text)  加号是把string转换为number类型
			};
		},
		isParentId:function(){
			if (this.parentId){
				return "have";
			}
		}
	},{itemMtpLink:itemMtp});
	$("#projectFieList").html(rendered);
	
}


/**
 * 生成面包屑
 * @param vo
 */
function buildCrumbs(vo){
	//分割路径的名字
	var pathName = vo.folderPathName;
	var pathNameArray = new Array();
	var pathIdArray = new Array();
	if(pathName){
		pathNameArray = pathName.split(",");
		pathIdArray = vo.folderPath.split(",");
	}
	pathNameArray.push(vo.folderName);	
	pathIdArray.push(vo.uuid);	
	//导航条的全部文件 变成超链接
	var skipProject = '<a href="#" class="allFile">全部文件</a>';
	var html = Mustache.render(skipProject);
	$("#skipProject").html(html);
	var pathSb = new StringBuffer();
	for(var i=0;i<pathIdArray.length;i++){
		pathSb.append(' <i class="icon-fixed-width icon-angle-right dark"></i> ');
		pathSb.append(' <a href="#"  key="').append(pathIdArray[i]).append('"> ');							
		pathSb.append(pathNameArray[i]);
		pathSb.append('</a>');
	}
	$("#navigationProject").html(pathSb.toString());
}

/***
 * 判断是否为文件夹
 * @param type
 * @returns {Boolean}
 */
function isFolderFn(type){
	return type=="40010001";
}

/**
 *  文件/文件夹点击事件
 *  @param fileId 文件ID
 *  @param isFolder 是否为文件夹
 */
function clickFolderFn(fileId,isFolder){
	fileId = fileId?fileId:"";
		
	if(isFolder){						
		window.location.href = ctx+"/jsp/cd/doc/projectFileList.jsp?parentId="+fileId;
	}else{	
		//下载文件
		
		Otod.file.download(fileId);
	}
}


function showCheckedCount(){
	var choiceNumbers = $('#projectFieList .selectCheck:checked').length;
	var html = "文件名";
	if(choiceNumbers>0){
		html = '<lable id="file_action_word">已选中'+choiceNumbers+'个文件/文件夹</lable>&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-xs icon-trash btn-checkBox buttonDelete" data-toggle="button" >删除</button>';
	}else{
		html = "文件名";
	}
	
	$("#fileName").html(html);	
	
}


//移动到公司文库文件
function moveFrimLibrary(uuid) {
	Otod.ajax({
		url: ctx + '/cd/doc/moveFrimLibrary.do',
		data:{uuid:uuid},
		type:"GET",
		successFn : function(data){	
			location.reload();
		}
	});
}


//删除文件夹或者文件 
function remove(){ 
	
	Otod.Msg.confirm('您确定要删除选中的文件或文件夹',"删除", delOk);
	function delOk(){
		
		var checkedArrays = $('#projectFieList .selectCheck:checked');
		var checkedStr = "";
		for (var i = 0; i < checkedArrays.length; i++) {
			if(i==0){
				checkedStr=checkedArrays[i].value;
			}else{
				checkedStr=checkedStr+","+checkedArrays[i].value;
			}
		}
		Otod.ajax({
				url: ctx + '/cd/doc/deleteFolder.do',
				data: {parentId:checkedStr},
				type:"GET",
				successFn : function(data) {
					enablePageClick();
					//重新加载一下数据
					loadFileList(parentId);
					$("#fileName").html("文件名");
					//删除之后让最上面的checkBox不选中
				}
		}); 
		
	}
	
	//恢复文件夹事件
	function enablePageClick(){
		var v = 0;
		//点击文件名
		$("#cdDocDivList").on("click",".folderCls",function(e){
			e.preventDefault();
			e.stopPropagation();			//阻止冒泡
			
			var fileId = $(this).attr("key");
			var folderType = $(this).attr("folderType");
			
			clickFolderFn(fileId,isFolderFn(folderType));
		});
	}
}
